#include "pch.h"
#include "RefractionEffect.h"
#include "RendererEngine.h"
namespace mini
{
	CRefractionEffect::CRefractionEffect()
	{
		mType = EntityType::RenderViewPort;
	}

	CRefractionEffect::~CRefractionEffect()
	{

	}

	void CRefractionEffect::buildRefCamera(const CPoint3F& eye, const CPoint3F& lookat)
	{
		setName("RefractionViewport");
		initViewPortSize(GEngine->getDeviceWidth(), GEngine->getDeviceHeight());
		setRendererFlag(RendererType::FrontRenderer);
		createBuildCamera("RefractionCamera", eye, lookat);
		auto pScene = GEngine->getCurrentScene();
		for (auto item : pScene->getRenderNodes())
		{
			addRenderNode(item);
		}
		pScene->addActor(this);
	}

	void CRefractionEffect::setClipPlane(const CPoint4F& clipPlane)
	{
		mClipPlane = &clipPlane.x;
	}

	void CRefractionEffect::buildRenderViewport()
	{
		auto pScene = GEngine->getCurrentScene();
		for (auto item : pScene->getRenderNodes())
		{
			auto renderNode = item->Clone();
			renderNode->setMaterialUnit(mpMaterialUnit);
			addRenderNode(renderNode);
		}
		pScene->addActor(this);
	}

	void CRefractionEffect::updateFrame(float fDeltaTime)
	{
		if (0 != mpMeshUnits.size())
		{
			auto materialUnit = mpMeshUnits.front()->getMaterialUnit();
			materialUnit->updateVSConstBuffer(5, &mClipPlane);
		}
	}

}